package com.qingcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.qingcheng.dao.TbBrandMapper;
import com.qingcheng.pojo.goods.TbBrand;
import com.qingcheng.service.TbBrandService;
import com.qingcheng.util.PageUtil;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Simon
* @date 2020/07/27
*/
@Service
public class TbBrandServiceImpl implements TbBrandService {

    @Resource
    private TbBrandMapper tbBrandMapper;

    @Override
    public List<TbBrand> queryAll() {
        Example example = new Example(TbBrand.class);
        example.setOrderByClause("id DESC");
        List<TbBrand> tbBrand = tbBrandMapper.selectByExample(example);
        return tbBrand;
    }

    @Override
    public PageUtil<TbBrand> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbBrand> pages = (Page<TbBrand>) tbBrandMapper.selectAll();
        PageUtil<TbBrand> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }

    /**
     * 分页查询
     *
     * @param tbBrand    关键字查询
     * @param pageNum    页数
     * @param pageSize   每页数量
     * @return 分页数据
     */
    @Override
    public PageUtil<TbBrand> pageQueryByAll(Integer pageNum, Integer pageSize, TbBrand tbBrand) {
        Example example=new Example(TbBrand.class);
        Example.Criteria criteria = example.createCriteria();

        if(tbBrand != null){
            if(!StringUtils.isEmpty(tbBrand.getId()))
                criteria.andEqualTo("id",tbBrand.getId());
            if(!StringUtils.isEmpty(tbBrand.getName()))
                criteria.andLike("name","%"+tbBrand.getName()+"%");
            if(!StringUtils.isEmpty(tbBrand.getLetter()))
                criteria.andEqualTo("letter",tbBrand.getLetter());
        }


        PageHelper.startPage(pageNum,pageSize);
        Page<TbBrand> pages = (Page<TbBrand>) tbBrandMapper.selectByExample(example);
        return new PageUtil<TbBrand>(pages.getTotal(),pages.getResult());
     }


    @Override
    public boolean saveTbBrand(TbBrand tbBrand) {
        int insert = tbBrandMapper.insert(tbBrand);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbBrand(TbBrand tbBrand) {
        int i = tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbBrandMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbBrand findById(Integer id) {
        TbBrand tbBrand = tbBrandMapper.selectByPrimaryKey(id);
        return tbBrand;
    }
}
